home *** CD-ROM | disk | FTP | other *** search
- Path: news0.accent.net!news
- From: Daniel Bolduc <eros-ssm@accent.net>
- Newsgroups: comp.lang.c
- Subject: C preprocessor problem
- Date: Sun, 03 Mar 1996 16:35:10 -0800
- Organization: EROS Inc.
- Message-ID: <313A3ABE.11CD@accent.net>
- NNTP-Posting-Host: 205.236.55.80
- Mime-Version: 1.0
- Content-Type: text/plain; charset=iso-8859-1
- Content-Transfer-Encoding: 8bit
- X-Mailer: Mozilla 2.0 (Win16; I)
-
- In the past, I programmed with the release 3.2.4 of SCO Operating and
- Development System. The C compiler was Microsoft C 6.0.
-
- With this compiler, i can use the maximum of the preprocessor without
- problem.
- The preprocessor is recursive, he have to resolve expression until the
- code
- is reduce to the simple code. With "cc" command i never had any error.
-
- But now, with the new compiler of SCO OpenServer, it didn't resolve the
- expression recursively.
- It translate expression with the appropriate code, but if the code use a
- variable, the reference is not resolve and i get a compiler error.
-
- I make an example of little source code to show the problem. The problem
- is coming with the translate command "?_FVAL( alias, field )".
- The expression "S_FVAL( Order, iNumber )"
- generated this "zaoFldOrder[ iNumberOrder ].u.cpValue"
-
- The reference of "iNumberOrder" is known, but the compiler don't
- resolve it. But if i write the code correctly like a result of
- preprocess,
- the compiler don't give an error.
-
- Does it have a setup to "cc" to eliminate this problem ?
-
- If i compile the source with the parameters "-E -P" and i dump the output
- on other file.c and I compile again the second file, I resolve the
- problem.
-
-
- //========BEGIN OF TEST.C================================================
- // test.c
- // Daniel Bolduc, March 3, 1996
-
- #include <stdio.h>
- #include <stdlib.h>
-
- #define ON 1
- #define OFF 0
-
- typedef int logic;
- typedef int bitmask;
- typedef char string;
-
- #define function
- #define local
-
- #define min(a,b) ( ( (a) < (b) ) ? (a) : (b) )
- #define max(a,b) ( ( (a) > (b) ) ? (a) : (b) )
-
- #define Min min
- #define Max max
-
- #define BoucleFor(_i_,_NbElem_) for( _i_=0; _i_ < _NbElem_ ; _i_++ )
-
- //=========================================================================
- // These defines produce the following result:
- //
- // DATABASE_RECORD( Employee )
- //
- // preprocessed code is:
- //
- // static int ziAreaEmployee = -1; static sFldEmployee [] = {
-
- #define DATABASE_RECORD(idAlias) \
- static int ziArea##idAlias = -1; \
- static sFld zaoFld##idAlias [] = {
-
- //=========================================================================
- // These defines produce the following result:
- //
- // USE_FIELD( cName, STRING )
- //
- // preprocessed code is:
- //
- // { "cName", STRING },
-
- #define USE_FIELD(idFldName,idType) \
- { #idFldName, idType },
-
- #define END_OF_RECORD \
- { 0 } };
-
- //=========================================================================
- // These defines produce the following result:
- //
- // S_FVAL( Employee, cName )
- //
- // preprocess code is:
- //
- // zaoFldEmployee [cNameEmployee] . u . cpValue
-
- #define S_FVAL(idAlias,idFld) \
- zaoFld##idAlias[##idFld##idAlias].u.cpValue
- #define C_FVAL(idAlias,idFld) \
- zaoFld##idAlias[##idFld##idAlias].u.cValue
- #define I_FVAL(idAlias,idFld) \
- zaoFld##idAlias[##idFld##idAlias].u.iValue
- #define L_FVAL(idAlias,idFld) \
- zaoFld##idAlias[##idFld##idAlias].u.lValue
- #define R_FVAL(idAlias,idFld) \
- zaoFld##idAlias[##idFld##idAlias].u.rValue
-
- //=========================================================================
- #define STRING 0
- #define CHAR 1
- #define INTEGER 2
- #define LOGICAL 3
- #define FLOAT 4
-
- static string *sacpType[] = { "STRING", "CHAR", "INTEGER", "LOGICAL",
- "FLOAT" };
-
- //=========================================================================
- typedef struct
- {
- string *cFieldName;
- int iType;
- union
- {
- string *cpValue;
- char cValue;
- int iValue;
- logic lValue;
- float rValue;
- } u;
- } sFld;
-
- //=========================================================================
-
- DATABASE_RECORD( Test )
- USE_FIELD( iNum, INTEGER )
- USE_FIELD( cName, STRING )
- USE_FIELD( cSex, CHAR )
- USE_FIELD( lActive, LOGICAL )
- USE_FIELD( rSalary, FLOAT )
- END_OF_RECORD
-
- enum
- {
- iNumTest, cNameTest, cSexTest, lActiveTest, rSalaryTest
- };
-
- //=========================================================================
-
- function main()
- {
- local int liX;
- local sFld *lopFld;
-
- I_FVAL( Test, iNum ) = 2240; // line
- 123
- S_FVAL( Test, cName ) = "Daniel Bolduc"; // line
- 124
- C_FVAL( Test, cSex ) = 'M'; // line
- 125
- L_FVAL( Test, lActive ) = ON; // line
- 127
- R_FVAL( Test, rSalary ) = 19.75; // line
- 128
-
- lopFld = zaoFldTest;
- BoucleFor( liX, 5 )
- {
- printf( "%-20s %-20s Value='"
- , lopFld->cFieldName
- , sacpType [lopFld->iType]
- );
- switch( lopFld->iType )
- {
- case STRING: printf( "%s'", lopFld->u.cpValue ); break;
- case CHAR: printf( "%c'", lopFld->u.cValue ); break;
- case INTEGER: printf( "%d'", lopFld->u.iValue ); break;
- case LOGICAL: printf( "%s'", lopFld->u.lValue ? "ON" : "OFF" );
- break;
- case FLOAT: printf( "%f'", lopFld->u.rValue ); break;
- }
- printf( "\n" );
- ++lopFld;
- }
- }
- //========END OF TEST.C================================================
-
- After doing this command, i got these errors:
-
- #cc test.c
-
- "test.c", line 123: error: invalid token: [iNumTest
- "test.c", line 123: error: Syntax error before or at: ]
- "test.c", line 124: error: invalid token: [cNameTest
- "test.c", line 125: error: invalid token: [cSexTest
- "test.c", line 126: error: invalid token: [lActiveTest
- "test.c", line 127: error: invalid token: [rSalaryTest
-
-
- If i compile the source like this, the program works:
-
- #cc -E -P test.c > test2.c
-
- "test.c", line 123: error: invalid token: [iNumTest
- "test.c", line 123: error: Syntax error before or at: ]
- "test.c", line 124: error: invalid token: [cNameTest
- "test.c", line 125: error: invalid token: [cSexTest
- "test.c", line 126: error: invalid token: [lActiveTest
- "test.c", line 127: error: invalid token: [rSalaryTest
-
- #cc test2.c
- #a.out
-
- iNum INTEGER Value='2240'
- cName STRING Value='Daniel Bolduc'
- cSex CHAR Value='M'
- lActive LOGICAL Value='ON'
- rSalary FLOAT Value='19.750000'
-
- --
- |\/\/\/|
- | |
- | | Daniel Bolduc
- | (o)(o) eros-ssm@accent.net
- C _) MontrĪal, QC, CANADA
- | ,___|
- | /
- /____\
- / \
-